<?php

namespace App\Trip\Orders\BookOrders\Train\SelectedSeats;

use App\Models\Train\TrainOrderPassengers\TrainOrderPassenger;
use App\Trip\Train\Seat\Seats;

class MatchSeatInfo
{
    private int   $current_count;
    private array $seat;
    private array $matched_pool = [];

    public function __construct(array $seat)
    {
        $this->seat          = $seat;
        $this->current_count = $this->getCount();
    }

    /**
     * 当前座位的选则数量
     *
     * @return int
     */

    public function getCount(): int
    {
        return $this->seat['count'];
    }

    /**
     * 匹配出票的坐席
     *
     * 如果是硬座 直接比较
     * 如果是卧铺 转化后比较
     *
     * @param TrainOrderPassenger $passenger
     *
     * @return bool
     */
    public function match(TrainOrderPassenger $passenger): bool
    {
        $ticket_seat = $passenger->getTicketSeat();
        if ($this->getCurrentCount() < 1) {
            return false;
        }
        if (!$this->isMatch($ticket_seat)) {
            return false;
        }

        $match_pool_key = $passenger->subPassengerId();
        if (isset($this->matched_pool[$match_pool_key])) {
            return false;
        }
        $this->matched_pool[$match_pool_key] = new MatchedPassengers($passenger, $this);
        $this->current_count--;
        return true;
    }

    /**
     * 匹配完剩余的数量
     *
     * @return int
     */
    public function getCurrentCount(): int
    {
        return $this->current_count;
    }

    /**
     * 是否能匹配上
     * 1 匹配类型
     * 2 匹配seats
     *
     * @param string $ticket_seat
     *
     * @return bool
     */
    public function isMatch(string $ticket_seat): bool
    {
        $selected_seat_type = Seats::SEAT_MAP_TO_TYPE[$ticket_seat];
        if ($this->getSeatType() != $selected_seat_type) {
            // 类型不匹配
            return false;
        }

        /***
         * 匹配内容 seats
         * 如果是卧铺 seat => 上中下铺
         */

        if ($selected_seat_type == 2) {
            return Seats::TRAIN_BEDS[$this->getSeat()] == $ticket_seat;
        }

        $seats_array = explode(',', $this->getSeats());
        return in_array($ticket_seat, $seats_array);
    }

    /**
     *  1 坐席 2 铺席
     * @return int
     */
    public function getSeatType(): int
    {
        return $this->seat['seat_type'];
    }

    /**
     * 座位 ABCDEF | 1,2,3 | 过道 ,窗
     *
     * @return string
     */
    public function getSeat(): string
    {
        return $this->seat['seat'];
    }

    /**
     * 过道靠窗会对应多个选则
     *
     * @return string
     */
    public function getSeats(): string
    {
        return $this->seat['seats'];
    }

    /**
     * 已匹配的乘客池 主要防止重复消费
     *
     * @return array
     */
    public function getMatchedPool(): array
    {
        return $this->matched_pool;
    }

    /**
     * 当前服务费
     *
     * @return float
     */
    public function getServiceFee(): float
    {
        return $this->seat['service_fee'];
    }

    /**
     * @return string
     */
    public function getSeatName(): string
    {
        if ($this->getSeatType() == Seats::SEAT_TYPE_SEAT) {
            if (in_array($this->getSeat(), Seats::SEAT_SIDES)) {
                return $this->getSeat();
            } else {
                return $this->getSeat()."座";
            }
        } elseif ($this->getSeatType() == Seats::SEAT_TYPE_BED) {
            return Seats::TRAIN_BEDS[$this->getSeat()];
        }

        return $this->getSeat();
    }
}